﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FTPFileDrop.Properties;
using Algobox.JuicyLib.Apps.Sterling;
using Algobox.JuicyLib.Apps.FTEN;

namespace FTPFileDrop
{           
    class Program
    {
        // default connection string
        // Data Source=localhost;Initial Catalog=LiquidView;Integrated Security=True

        // local connection string over VPN
        // Data Source=liqsql01b;Initial Catalog=LiquidView;User ID=beic;Password=blah

        const string FTEN_FILES = "-FTEN";
        const string STI_FILES = "-STI";
        const string DATE = "-DATE=";

        static void Main(string[] args)
        {
            Console.WriteLine("Starting " + System.Reflection.Assembly.GetExecutingAssembly().FullName);
            Console.WriteLine("Using config file: " + AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

            DateTime fileDate;
            string dateString = args.FirstOrDefault(a => a.Contains(DATE));

            if (dateString != default(string))
            {
                int dateIndex = dateString.IndexOf(DATE) + 1;
                if (!DateTime.TryParseExact(dateString.Substring(dateIndex),
                    "yyyyMMdd",
                    System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.DateTimeStyles.None,
                    out fileDate))
                {
                    Console.WriteLine("Command line switches: -FTEN -STI -DATE=yyyyMMdd");
                    return;
                }
            }
            else
            {
                fileDate = DateTime.Today.AddDays(-1);
            }

            foreach (string arg in args)
            {
                switch (arg)
                {
                    case FTEN_FILES:
                        using (FTENFileDrop dropper = new FTENFileDrop(Settings.Default.DBConString))
                        {
                            Console.WriteLine("FTEN files for " + fileDate.ToShortDateString());

                            dropper.RunPositionFileDrop(
                                fileDate,
                                Settings.Default.FTEN_FTPDestinationURI,
                                Settings.Default.FTEN_FTPUsername,
                                Settings.Default.FTEN_FTPPassword);

                            dropper.RunFXFileDrop(
                                fileDate,
                                Settings.Default.FTEN_FTPDestinationURI,
                                Settings.Default.FTEN_FTPUsername,
                                Settings.Default.FTEN_FTPPassword);
                        }
                        break;

                    case STI_FILES:

                        Console.WriteLine("Sterling files for " + fileDate.ToShortDateString());

                        List<string> gbpSymbols = new List<string>();
                        Settings.Default.GBPSymbols.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach( (a) => { gbpSymbols.Add(a.Trim()); });

                        using (SterlingFileDrop dropper = new SterlingFileDrop(gbpSymbols, Settings.Default.DBConString))
                        {
                            dropper.RunPositionFileDrop(
                                fileDate,
                                Settings.Default.STI_FTPDestinationURI, 
                                Settings.Default.STI_FTPUsername, 
                                Settings.Default.STI_FTPPassword);

                            dropper.RunNetLiqFileDrop(
                                fileDate,
                                Settings.Default.STI_FTPDestinationURI,
                                Settings.Default.STI_FTPUsername,
                                Settings.Default.STI_FTPPassword);
                        }
                        break;

                    default:
                        Console.WriteLine("Command line switches: -FTEN -STI -DATE=yyyyMMdd");
                        return;
                }

            }
        }
    }
}
